//
//  KYZXVideoVC.m
//  KuYouZiXun
//
//  Created by lanou on 15/8/10.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "KYZXVideoVC.h"

#import "KYZXNodeModel.h"

#import "KYZXScreen.h"

#import "KYZXVideoHeadlineVC.h"

#import "KYZXVideoCollectionVC.h"
@interface KYZXVideoVC ()

@end

@implementation KYZXVideoVC

- (NSMutableArray *)nodeData{
    
    if (_nodeData == nil) {
        _nodeData = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _nodeData;
}

- (void)papareData{
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"videoNode" ofType:@"json"];
    //2.获取文件二进制流
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    //3.解析数据
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
    NSArray *resultArray = dataDic[@"result"];
    for (NSDictionary *dic in resultArray) {
        KYZXNodeModel *node = [[KYZXNodeModel alloc] init];
        [node setValuesForKeysWithDictionary:dic];
        [self.nodeData addObject:node];
    }
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self createScrollView];
    [self createTopScrollView];
    //解决scrollView 上面手势冲突 关键代码:self.scrollView的手势和self.edgePan重合时self.edgePan优先
    [self.scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.edgePan];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    }



#pragma mark 根据传进来的数据动态创建scrollView
- (void)createScrollView{
    
    [self papareData];
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.contentView.bounds];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    [self.contentView addSubview:self.scrollView];
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * self.nodeData.count, self.contentView.bounds.size.height);
    

    for (int i = 0; i < self.nodeData.count; i++) {
        if (i == 0) {
            //头条视图
            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
            layout.minimumInteritemSpacing = 0;
            layout.minimumLineSpacing = 5;
            layout.itemSize = CGSizeMake(SCREEN_WIDTH / 2.0, SCREEN_WIDTH / 8.0 * 3.2);
            KYZXVideoHeadlineVC *headlineVC = [[KYZXVideoHeadlineVC alloc] initWithCollectionViewLayout:layout];
            headlineVC.view.frame = CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, self.contentView.bounds.size.height);
            headlineVC.videoVC = self;
            
            [self.scrollView addSubview:headlineVC.view];
            [self addChildViewController:headlineVC];
        }
         //先把vc创建出来 后面懒加载请求数据
        else{
            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
            layout.minimumInteritemSpacing = 0;
            layout.minimumLineSpacing = 5;
           layout.itemSize = CGSizeMake(SCREEN_WIDTH / 2.0, SCREEN_WIDTH / 8.0 * 3.2);
            KYZXVideoCollectionVC *collectionVC = [[KYZXVideoCollectionVC alloc] initWithCollectionViewLayout:layout];
            [self addChildViewController:collectionVC];

        }
    }
}


//主视图滑动结束,顶栏视图随之变化
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //
    NSLog(@"滑动结束");
    int i = scrollView.contentOffset.x / SCREEN_WIDTH;
    if (self.selectedNodeTag != 1000000 + i) {
        UIButton *proviousButton = (UIButton *)[self.topScrollView viewWithTag:self.selectedNodeTag];
        proviousButton.selected = NO;
        UIButton *selectedButton = (UIButton *)[self.topScrollView viewWithTag:1000000 + i];
        selectedButton.selected = YES;
        self.selectedNodeTag = selectedButton.tag;
        [self adjustScrollViewContentX:selectedButton];
    }
    //第0个是头条 如果不是头条 滑动过去发现数据为空,那我们就请求数据 相当于懒加载吧
    if (i > 0) {
        KYZXVideoCollectionVC *collectionVC = self.childViewControllers[i];
        if(collectionVC.dataSource.count == 0){
            NSLog(@"%d没数据",i);
            collectionVC.nodeModel = self.nodeData[i];
            collectionVC.view.frame = CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, self.contentView.bounds.size.height);
            collectionVC.videoVC = self;
            [self.scrollView addSubview:collectionVC.view];
        }
    }
}


//是顶栏选中的按钮不会超出顶栏滑动视图范围
- (void)adjustScrollViewContentX:(UIButton *)button
{
    CGPoint newContentOffset = _topScrollView.contentOffset;
    //将选中状态的button传进来,如他的最左边 当前的contentOffset.x 就让顶栏滑动视图向右边移动一点,大小为 contentOffset.x - button.frame.origin.x
    if (button.frame.origin.x < newContentOffset.x) {
        newContentOffset.x = newContentOffset.x - (newContentOffset.x - button.frame.origin.x);
    }
    //如果他的最右边大于当前的contentOffset.x + 顶栏滑动视图的宽度 就让顶栏滑动视图向左移动一点,大小为
    if (CGRectGetMaxX(button.frame) > (_topScrollView.frame.size.width + newContentOffset.x)){
        newContentOffset.x = newContentOffset.x + (CGRectGetMaxX(button.frame) - (_topScrollView.frame.size.width + newContentOffset.x));
    }
    [_topScrollView setContentOffset:newContentOffset animated:YES];
}


#pragma mark 创建顶栏滑动视图 button tag = button.tag = 1000000 + i;
- (void)createTopScrollView
{
    self.topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(44, 0, SCREEN_WIDTH - 44, 44)];
    _topScrollView.showsHorizontalScrollIndicator = NO;
    [self.topBarView addSubview:_topScrollView];
    _topScrollView.contentSize = CGSizeMake(44 * self.nodeData.count, 44);
    for (int i = 0; i < self.nodeData.count; i++) {
        KYZXNodeModel *nodeModel = self.nodeData[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 1000000 + i;
        //默认选择0
        if(i == 0){
            _selectedNodeTag = button.tag;
            button.selected = YES;
        }
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
         [button setBackgroundImage:[UIImage imageNamed:@"red_line_and_shadow.night"] forState:UIControlStateSelected];
        [button setTitle:nodeModel.nodeName forState:UIControlStateNormal];
        button.frame = CGRectMake(44 * i, 0, 44, 44);
        [button addTarget:self action:@selector(changeContentView:) forControlEvents:UIControlEventTouchUpInside];
        [self.topScrollView addSubview:button];
    }
}


//点击顶栏上面的button使下面的scrollView移动到指定的内容
- (void)changeContentView:(UIButton *)button{
    
    int i = button.tag - 1000000;
    UIButton *proviousButton = (UIButton *)[self.topScrollView viewWithTag:_selectedNodeTag];
    proviousButton.selected = NO;
    _selectedNodeTag = button.tag;
    button.selected = YES;
    CGRect newFrame = self.scrollView.frame;
    newFrame.origin.x = i * SCREEN_WIDTH;
    //点击按钮 直接调到指定位置 手动调用滑动结束方法 获取数据
    [self.scrollView scrollRectToVisible:newFrame animated:NO];
    [self scrollViewDidEndDecelerating: self.scrollView];
    [self adjustScrollViewContentX:button];
}

@end
